home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-04 | 17.5 KB | 647 lines | [TEXT/KAHL] |
-
- //• C translation from Pascal source file: CONTROL.p
-
- //• $X-.
- //• Turn off stack expansion. This is a Lisa concept, not needed on Mac.
- //• $U-.
- //• Turn off the Lisa Libraries. This is required by the WorkShop.
- //• $R-.
- //• Turn off range checking.
-
- //• main ScrollControls;
-
- //• Jeffery J. Bradford Macintosh Technical Support April 1985 .
-
- //• This example will help you become familiar with how scroll bars work.
- //• and allow you manipulate the contents of a window. This particular .
- //• example remembers where the drawing origin should be and sets the .
- //• origin to that spot before drawing every time. This involves some .
- //• book keeping, but we've made it pretty simple and minimized globals..
-
-
- //• includes:
- Types, QuickDraw, OSUtils, ToolUtils, Packages;
-
- enum {
- //• menu stuff.
- AppleMenu == 256;
- FileMenu == 257;
- EditMenu == 258;
-
- //• window IDs.
- WindResID == 256;
-
- //• scroll bar res IDs.
- VScrollBar == 256; //• res ID for bottom/horz scroll bar.
- HScrollBar == 257; //• res ID for side/vert scroll bar.
-
- //• control stuff.
- didntFindaControl == 0; //• used in IF statements for clarity.
- didntMove == 0; //• used in IF statements for clarity.
- VSBarWidth == 15; //• width of vertical scroll bar.
- HSBarHeight == 15; //• width of horz. scroll bar.
- Line_Inc == 5; //• amount of scrolling per button press.
- Page_Inc == 100; //• amount of scrolling per page press.
- HorzScrollBar == 1; //• used to distinguish between vert & horz scroll bar.
- VertScrollBar == 0; //• used to distinguish between vert & horz scroll bar.
-
-
-
-
- //• this is useful stuff you might need sometime.
-
- typedef //• packed//• Expected ;.
- WordStuff;
- //• Expected type identifier.
- //• Expected =.
- typedef //• Expected type.
- //• Expected ;.
- switch (;
- //• Expected =.
- typedef enum {
- Word: short
- } 0;
- //• Expected =.
- typedef enum {
- SByte1, SByte0: SignedByte
- } 1;
- //• Expected =.
- typedef enum {
- b15, b14, b13, b12, b11, b10, b9, b8, b7, b6, b5, b4, b3, b2, b1, b0: Boolean
- //• Expected ;.
- }//• Expected ;.
- 2;//• Expected type identifier.
-
-
- //• Expected =.
- typedef //• Expected type.
- //• Expected ;.
- ;; //• Expected =.
- typedef chr3//• Expected ;.
- //• packed; //• Expected =.
- typedef chr1//• Expected ;.
- chr2; //• Expected =.
- typedef char chr0;
- };
-
- LMwordPtr == [0]short; //• pointer to low memory address.
- LMLongPtr == [0]long; //• pointer to low memory address - long.
-
-
-
-
- //• global program stuff.
- Boolean Finished; //• used to terminate the program.
- CursHandle ClockCursor; //• handle to the waiting watch cursor.
-
- //• Screen stuff.
- Rect DragArea; //• holds the area where window can be dragged in.
- Rect GrowArea; //• holds the area to which a window's size can change.
- Rect Screen; //• holds the screen dimensions.
-
- //• Window stuff.
- WindowPtr OneWindow; //• pointer to the first window.
- Rect UsableArea; //• uasable area of the windows content region.
-
- //• -----------------------------------------------------------------------------.
- //• end of global variable definition.
- //• -----------------------------------------------------------------------------.
-
- void DrawBullseye()
- {
-
- Rect TempRect;
- short i, a, b, x;
- {
- MoveTo(0, 0);
- LineTo(700, 700);
- MoveTo(0, 350);
- LineTo(700, 350);
- MoveTo(0, 700);
- LineTo(700, 0);
- MoveTo(350, 0);
- LineTo(350, 700);
-
- a = 0;
- b = 700;
- for ( i = 0 ; • < 11 )
- {
- x = 56 - i * 5;
- a = a + x;
- b = b - x;
- SetRect(TempRect, a, a, b, b);
- FrameRect(TempRect);
- };
-
- FillRect(TempRect, black);
- };
-
- //• -----------------------------------------------------------------------------.
-
- void DrawDisplay()
- {
-
- Rect TempRect; //• used for offseting the Usable display area.
- Point CurOrigin; //• holds the current origin.
-
- {
- //• in order to confine the drawing to the usable content area of the window.
- //• we will set a clipRect of that area.
-
- TempRect = UsableArea;
- CurOrigin = Point(GetWRefCon(OneWindow)); //• get the current origin.
-
- //• set up to draw properly, and change the clip.
- SetOrigin(CurOrigin.h, CurOrigin.v); //• account for scrolling.
- OffsetRect(TempRect, CurOrigin.h, CurOrigin.v); //• account for scrolling.
- ClipRect(TempRect);
-
- DrawBullseye;
-
- SetOrigin(0, 0); //• reset it back to normal.
- ClipRect(OneWindow->portRect); //• reset it to the original window.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void ScrolltheScreen(ControlHandle cntlHdl)
- {
-
- Point Origin; //• holds the coordinates of the new & old origin.
- Point OldOrigin; //• holds the coordinates of the old/last origin.
- Point NewOrigin; //• holds the coordinates of the new origin.
- short delta_H;
- short delta_V;
- RgnHandle UpDateRgn; //• holds the region to be scrolled.
-
- {
-
- //• Code below is for clarity}
- {{get the current (or old) origin}
- {OldOrigin := Point(GetWRefCon(OneWindow));}
-
- {get the NEW origin, we can only scroll in one direction at a time so}
- {if GetCRefCon(C
-
- Origin = Point(GetWRefCon(OneWindow));
- if ( GetCRefCon(CntlHdl) == VertScrollBar )
- {
- delta_H = 0; //• didn't change.
- delta_V = Origin.v - GetCtlValue(CntlHdl); //• get the diff.
- Origin.v = GetCtlValue(CntlHdl); //• set to new position.
- }
- else
- {
- delta_V = 0; //• didn't change.
- delta_H = Origin.h - GetCtlValue(CntlHdl); //• get the diff.
- Origin.h = GetCtlValue(CntlHdl); //• set to new position.
- };
-
-
- //• get a region for use in updating/scrolling the screen.
- UpdateRgn = NewRgn ();
- ScrollRect(UsableArea, delta_H, delta_V, updateRgn);
-
- SetOrigin(Origin.h, Origin.v); //• set to the new origin.
-
- //• move the update Rgn to new location.
- OffsetRect(updateRgn[0]->rgnBBox, Origin.h, Origin.v);
-
- //• clip to it.
- ClipRect(updateRgn[0]->rgnBBox);
-
- DrawBullseye; //• do the drawing.
-
- //• reset everything back to normal & save the new origin.
- SetOrigin(0, 0);
- ClipRect(OneWindow->portRect);
- SetWRefCon(OneWindow, long(Origin));
-
- DisposeRgn(updateRgn);
- };
-
- //• -----------------------------------------------------------------------------.
-
- void LineScroll(ControlHandle SBar, short ControlLoc)
- {
- {
- if ( ControlLoc == inUpButton )
- SetCtlValue(SBar, GetCtlValue(SBar) - Line_inc)
- else
- SetCtlValue(SBar, GetCtlValue(SBar) + Line_inc);
-
- ScrolltheScreen(SBar);
- };
-
- //• -----------------------------------------------------------------------------.
-
- void HandleControl_at(Point atMouseLoc, WindowPtr inThisWindow)
- {
-
- ControlHandle CntlHdl; //• handle of a control part selected.
- short ControlLoc; //• position ID of the control part selected.
- short dummy; //• used to hold integer returned from TrackControl.
-
- {
- ControlLoc = FindControl(atMouseLoc, inThisWindow, CntlHdl);
-
- if ( ControlLoc != didntFindaControl )
- switch ( ControlLoc )
- {
-
- inUpButton:
- dummy = TrackControl(CntlHdl, atMouseLoc, @LineScroll);
-
- inDownButton:
- dummy = TrackControl(CntlHdl, atMouseLoc, @LineScroll);
-
- inPageUp:
- {
- SetCtlValue(CntlHdl, GetCtlValue(CntlHdl) - Page_inc);
- ScrolltheScreen(CntlHdl);
- };
-
- inPageDown:
- {
- SetCtlValue(CntlHdl, GetCtlValue(CntlHdl) + Page_inc);
- ScrolltheScreen(CntlHdl);
- };
-
- inThumb:
- if ( TrackControl(CntlHdl, atMouseLoc, 0L) != didntMove )
- ScrolltheScreen(CntlHdl);
-
- };//• case of control location.
- };//• of Handle Window Controls.
-
- //• -----------------------------------------------------------------------------.
-
- void ProcessMenu_in(long CodeWord)
- {
-
- short Menu_No; //• menu number that was selected.
- short Item_No; //• item in menu that was selected.
- Str255 NameHolder; //• name holder for desk accessory or font.
- short DNA; //• OpenDA will never return 0, so don't care.
-
- {
- if ( CodeWord != 0 ) //• go ahead and process the command.
- {
- Menu_No = HiWord(CodeWord); //• get the Hi word of....
- Item_no = LoWord(CodeWord); //• get the Lo word of....
-
- switch ( Menu_No )
- {
-
- AppleMenu:
- {
- GetItem(GetMHandle(AppleMenu), Item_No, NameHolder);
- DNA = OpenDeskAcc(NameHolder);
- };
-
- FileMenu:
- {
- switch ( Item_No )
- {
- 1:
- Finished = true; //• quit.
- };
- };
-
- EditMenu:
- {
- if ( ! SystemEdit(Item_no - 1) ) //• if not for a desk accessory.
- switch ( Item_No )
- {
- 1:
- {
- ;}; //• undo.
- //• 2: line divider.
- 3:
- {
- ;}; //• cut.
- 4:
- {
- ;}; //• copy.
- 5:
- {
- ;}; //• paste.
- 6:
- {
- ;}; //• clear.
- };
- };
-
- };//• case of Menu_No.
-
- HiliteMenu(0); //• unhilite after processing menu.
- }; //• the If codeword <> 0.
- }; //• of ProcessMenu_in procedure.
-
-
- //• -------------------------------------------------------------------.
- //• ----- These are procedures called from the main event loop -------.
-
- void DealwthMouseDowns(EventRecord Event)
- {
-
- WindowPtr WindowPointedTo;
- Point MouseLoc;
- short WindoLoc;
- {
- MouseLoc = Event.Where;
- WindoLoc = FindWindow(MouseLoc, WindowPointedTo);
- switch ( WindoLoc )
- {
-
- inMenuBar:
- ProcessMenu_in(MenuSelect(MouseLoc));
-
- inSysWindow:
- SystemClick(Event, WindowPointedTo);
-
- inContent:
- if ( WindowPointedTo != FrontWindow () )
- SelectWindow(WindowPointedTo)
- else
- {
- GlobaltoLocal(MouseLoc);
- if ( ! PtinRect(MouseLoc, UsableArea) )
- HandleControl_at(MouseLoc, WindowPointedTo)
- else
- InvalRect(UsableArea); //• just for effect.
- };
-
- inGrow:
- if ( WindowPointedTo != FrontWindow () )
- SelectWindow(WindowPointedTo)
- else
- { //• ReSizeWindow(WindowPointedTo,MouseLoc);.
- ;};
-
- inDrag:
- DragWindow(WindowPointedTo, MouseLoc, DragArea);
-
- inGoAway:
- if ( TrackGoAway(WindowPointedTo, MouseLoc) )
- DisposeWindow(WindowPointedTo); //• since W mgr allocated space.
-
- };//• of case.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void DealwthKeyDowns(EventRecord Event)
- {
-
- char CharCode;
- {
- CharCode = CharStuff(Event.message).Chr0; //• get low byte w/no processing.
-
- if ( (Event.message, cmdKey) == cmdKey )
- { //• key board command - probably a menu command.
- ProcessMenu_in(MenuKey(CharCode));
- }
- else
- {
- //• regular keyboard entry.
- ;};
- };
-
- //• -----------------------------------------------------------------------------.
-
- void DealwthActivates(EventRecord Event)
- {
-
- WindowPtr TargetWindow;
- WindowPeek TargetPeek;
- ControlHandle CntrlHdl;
- {
- TargetWindow = WindowPtr(Event.message);
- TargetPeek = WindowPeek(TargetWindow);
- DrawGrowIcon(TargetWindow);
-
- if ( Odd(Event.modifiers) )
-
- //• the window is becoming active.
- {
- //• 1.
- SetPort(TargetWindow);
-
- //• 2.
- //• activate the window controls.
- CntrlHdl = ControlHandle(TargetPeek->ControlList); //• get the first one.
- do
- {
- if ( CntrlHdl != 0L ) //• in case we get a nil on the first one.
- {
- ShowControl(CntrlHdl);
- CntrlHdl = CntrlHdl[0]->nextControl;
- };
- } while (! CntrlHdl == 0L;
-
- //• 3.
- //• activate selections, etc..
- }
-
-
- else //• the window is being DEactivatived.
- {
- //• 1.
- //• DEactivate the window controls.
- CntrlHdl = ControlHandle(TargetPeek->ControlList); //• get the first one.
- do
- {
- if ( CntrlHdl != 0L ) //• in case we get a nil on the first one.
- {
- HideControl(CntrlHdl);
- CntrlHdl = CntrlHdl[0]->nextControl;
- };
- } while (! CntrlHdl == 0L;
-
- //• 2.
- //• deactivate selections, etc.
- };
- };
-
- //• -----------------------------------------------------------------------------.
-
- void DealwthUpdates(EventRecord Event)
- {
-
- WindowPtr UpDateWindow, TempPort;
- {
- UpDateWindow = WindowPtr(Event.message);
- GetPort(TempPort); //• Save the current port.
-
- SetPort(UpDateWindow); //• set the port to one in Evt.msg.
- BeginUpdate(UpDateWindow);
- EraseRect(UsableArea);
- DrawDisplay;
- DrawControls(UpDateWindow);
- DrawGrowIcon(UpDateWindow);
- EndUpdate(UpDateWindow);
-
- SetPort(TempPort); //• restore to the previous port.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void MainEventLoop()
- {
-
- EventRecord Event;
- Boolean ProcessIt;
- {
- do
- {
- SystemTask (); //• so you can support Desk Accessories.
-
- ProcessIt = GetNextEvent(everyEvent, Event);
- if ( ProcessIt )//• is true.
- //• we'll ProcessIt.
- switch ( Event.what )
- {
-
- mouseDown:
- DealwthMouseDowns(Event);
- keyDown:
- DealwthKeyDowns(Event);
- ActivateEvt:
- DealwthActivates(Event);
- updateEvt:
- DealwthUpdates(Event);
-
- };//• of Case.
- } while (! Finished; //• terminate the program.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void InitThings()
- {
- {
- InitGraf(@thePort); //• create a grafport for the screen.
-
- MoreMasters (); //• extra pointer blocks at the bottom of the heap.
- MoreMasters (); //• this is 5 X 64 master pointers.
- MoreMasters ();
- MoreMasters ();
- MoreMasters ();
-
- //• get the cursors we use and lock them down - no clutter.
- ClockCursor = GetCursor(watchCursor);
- HLock(Handle(ClockCursor));
-
- //• show the watch while we wait for inits & setups to finish.
- SetCursor(ClockCursor[0][0]);
-
- //• init everything in case the app is the Startup App.
- InitFonts (); //• startup the fonts manager.
- InitWindows (); //• startup the window manager.
- InitMenus (); //• startup the menu manager.
- TEInit (); //• startup the text edit manager.
- InitDialogs(0L); //• startup the dialog manager.
-
- Finished = false; //• set program terminator to false.
- FlushEvents(everyEvent, 0); //• clear events from previous program.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void SetupLimits()
- {
- {
- Screen = ScreenBits.Bounds; //• set the size of the screen.
- SetRect(DragArea, Screen.left + 4, Screen.top + 24, Screen.right - 4, Screen.bottom - 4);
- SetRect(GrowArea, Screen.left, Screen.top + 24, Screen.right, Screen.bottom);
- };
-
- //• -----------------------------------------------------------------------------.
-
- void SetupMenus()
- {
-
- MenuHandle MenuTopic;
- {
- MenuTopic = GetMenu(AppleMenu); //• get the apple desk accessories menu.
- AddResMenu(MenuTopic, "DRVR"); //• adds all names into item list.
- InsertMenu(MenuTopic, 0); //• put in list held by menu manager.
-
- MenuTopic = GetMenu(FileMenu); //• always need this for Quiting.
- InsertMenu(MenuTopic, 0);
-
- MenuTopic = GetMenu(EditMenu); //• always need for editing Desk Accessories.
- InsertMenu(MenuTopic, 0);
-
- DrawMenuBar (); //• all done so show the menu bar.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void SetupWindows()
- {
-
- ControlHandle vScroll; //• used to create control.
- ControlHandle hScroll; //• same thing, we could put them in global but the.
- //• the window is global and it will have them.
- {
- OneWindow = GetNewWindow(WindResID, 0L, Ptr(-1));
- SetPort(OneWindow); //• so usableArea is correct.
-
- //• now setup the usable window area.
- UsableArea = OneWindow->portRect; //• initialize it.
- UsableArea.right = UsableArea.right - VSBarWidth;
- UsableArea.bottom = UsableArea.bottom - HSBarHeight;
-
- //• now set up the scroll bar controls that this window will use.
- vScroll = GetNewControl(VScrollBar, OneWindow); //• get vertical scroll bar.
- hScroll = GetNewControl(HScrollBar, OneWindow); //• get horizontal scroll bar.
-
- //• our display is going to be 700 by 700 pixels so set min & max values.
- //• Note: the window is 290 by 490.
- SetCtlMin(hScroll, 0); //• set the minimum value in vertical direction.
- SetCtlMax(hScroll, 210); //• set the maximum value in vertical direction.
- SetCtlMin(vScroll, 0); //• set the minimum value in horizontal direction.
- SetCtlMax(vScroll, 410); //• set the maximum value in horizontal direction.
-
- //• the current origin will be stored in the refcon of the window.
- //• NOTE: its already been set in the resource file}
- {SetWRefCon(OneWindow, 0); initialize it to zero*.
-
- //• all set so show the window.
- ShowWindow(OneWindow);
-
- //• Note that the controls are brought in invisibily, show them.
- //• when the window becomes active, hide them when its inactive.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void SetUpThings()
- {
- {
- SetupWindows; //• do first so its low in heap.
- SetupMenus;
- SetupLimits;
-
- InitCursor (); //• ready to go, so show the Arrow cursor.
- };
-
- //• -----------------------------------------------------------------------------.
-
- void CloseThings()
- {
- {
- //• close files, if you changed sys resources, UNchange them here be carefull.
- //• about changing sys things, remember the Switcher could be around.
- ;};
-
- //• -----------------------------------------------------------------------------.
-
- {
- InitThings;
- SetUpThings;
- MainEventLoop;
- CloseThings;
- end.